import * as path from "path";
import { EggAppConfig, EggAppInfo, PowerPartial } from "egg";

export default (appInfo: EggAppInfo) => {
  const config = {} as PowerPartial<EggAppConfig>;

  // override config from framework / plugin
  // use for cookie sign key, should change to your own and keep security
  config.keys = appInfo.name + "_1639447740934_9052";

  // add your egg config in here
  config.middleware = [];
  config.extend = ["helper"];
  config.bodyParser = {
    jsonLimit: "10mb",
  };
  // 文件上传解析 form/data
  config.multipart = {
    mode: "file",
  };
  // 静态文件
  config.static = {
    prefix: "/public/",
    dir: path.join(appInfo.baseDir, "app/public"),
    // dirs: [ dir1, dir2 ] or [ dir1, { prefix: '/static2', dir: dir2 } ],
    // support lazy load
    dynamic: true,
    preload: false,
    buffer: false,
    maxFiles: 1000,
  };
  //自定义 token 的加密条件字符串
  config.jwt = {
    secret: 'config.keys', 
  };
  // 安全策略选项，关闭暂时关闭csrf
  config.security = {
    xframe: {
      enable: false,
    },
    csrf: {
      enable: false,
    },
  };
  config.cors = {
    origin: "*",
    allowMethods: "GET,PUT,POST,DELETE",
  };
  config.session = {
    key: "EGG_SESS",
    maxAge: 24 * 3600 * 1000, // 1 天
    httpOnly: true,
    encrypt: true,
  };
  config.mysql = {
    client: {
      host: "localhost",
      port: "3306",
      user: "root",
      password: "123456",
      database: "MyBlog",
      dateStrings: true,
    },
    // 是否加载到 app 实例中，默认开启
    app: true,
    //  是否加载到 agent 中，默认关闭
    agent: false,
  };
  // add your special config in here
  const bizConfig = {
    sourceUrl: `https://github.com/eggjs/examples/tree/master/${appInfo.name}`,
  };
  const filePath = {
    articles: path.join(appInfo.baseDir, "app/public", "articles"),
  };
  // the return config will combines to EggAppConfig
  return {
    ...config,
    ...bizConfig,
    filePath,
  };
};
